﻿using System.Collections.Generic;
using System;
using System.Text;

namespace Ex03.GarageLogic
{
    /// <summary>
    /// This class is for an Car Data
    /// </summary>
    public class CarData
    {
        public enum eCarColor
        {
            Red,
            Green,
            Blue
        }

        public enum eNumberOfDoors
        {
            Two = 2,
            Three,
            Four,
            Five
        }
        
        private const string k_ColorKey = "Color";
        private const string k_NumberOfDoorsKey = "Number Of Doors";
        public const int k_NumberOfWheels = 4;
        public const float k_MaxAirPressureOfWheels = 29;
        private static readonly Dictionary<string, string> sr_CarDataForm;
        
        private eCarColor m_Color;
        private eNumberOfDoors m_NumberOfDoors;

        /// <summary>
        /// This is a static constructor that will initialize the Car Data
        /// form 
        /// </summary>
        static CarData()
        {
            sr_CarDataForm = Wheel.CreateWheelsForm(k_NumberOfWheels);
            sr_CarDataForm.Add(k_ColorKey, null);
            sr_CarDataForm.Add(k_NumberOfDoorsKey, null);
        }

        /// <summary>
        /// A constructor for the Car Data
        /// </summary>
        /// <param name="i_CreationForm">
        /// A key value Dictionary where key is the name of the member 
        /// to initialize and value is the new value
        /// <remarks>
        ///     The input form Must have a color property and a number of doors property
        ///     the input Must be "pars-able" to eCarColor and eNumberOfDoors
        ///     else will throw FormatException
        /// </remarks>
        /// </param>
        public CarData(Dictionary<string, string> i_CreationForm)
        {
            initailizeColorByForm(i_CreationForm);
            initailizeNumberOfDoorsrByForm(i_CreationForm);
        }

        private void initailizeColorByForm(Dictionary<string, string> i_CreationForm)
        {
            const bool v_IgnorCase = true;
            bool isGetValueSucceded; 
            string ColorString;
            eCarColor colorTemp;

            isGetValueSucceded = i_CreationForm.TryGetValue(k_ColorKey, out ColorString);
            if (!isGetValueSucceded)
            {
                throw new FormatException("Color is invalid");
            }

            try
            {
                colorTemp = (eCarColor)Enum.Parse(typeof(eCarColor), ColorString, v_IgnorCase);
            }
            catch (Exception exp)
            {
                throw new FormatException("Color is invalid");
            }

            m_Color = colorTemp;
        }

        private void initailizeNumberOfDoorsrByForm(Dictionary<string, string> i_CreationForm)
        {
            const bool v_IgnorCase = true;
            bool isGetValueSucceded;
            string numberOfDoorsString;
            eNumberOfDoors numberOfDoorsTemp;

            isGetValueSucceded = i_CreationForm.TryGetValue(k_NumberOfDoorsKey, out numberOfDoorsString);
            if (!isGetValueSucceded)
            {
                throw new FormatException("Number of doors is invalid");
            }

            try
            {
                numberOfDoorsTemp = (eNumberOfDoors)Enum.Parse(typeof(eNumberOfDoors), numberOfDoorsString, v_IgnorCase);
            }
            catch (Exception exp)
            {
                throw new FormatException("Number of doors is invalid");
            }

            m_NumberOfDoors = numberOfDoorsTemp;
        }

        /// <summary>
        /// A property to get the Car Data form
        /// <remarks>will return a new form</remarks>
        /// </summary>
        public static Dictionary<string, string> CarDataForm
        {
            get { return new Dictionary<string, string>(sr_CarDataForm); }
        }

        /// <summary>
        /// Return a string representation object members as pair 
        /// members name and variable value
        /// </summary>
        /// <returns>string "varName + Vehicle.k_ToStirngPairDelimiter + varValue + Vehicle.sr_ToStirngEndOfPairDelimiter"</returns>
        public override string ToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(colorToString());
            stringBuilderResult.Append(numberOfDoorsToString());

            return stringBuilderResult.ToString();
        }
        
        private string colorToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(k_ColorKey);
            stringBuilderResult.Append(Vehicle.k_ToStirngPairDelimiter);
            stringBuilderResult.Append(m_Color);
            stringBuilderResult.Append(Vehicle.sr_ToStirngEndOfPairDelimiter);

            return stringBuilderResult.ToString();
        }

        private string numberOfDoorsToString()
        {
            StringBuilder stringBuilderResult = new StringBuilder();

            stringBuilderResult.Append(k_NumberOfDoorsKey);
            stringBuilderResult.Append(Vehicle.k_ToStirngPairDelimiter);
            stringBuilderResult.Append(m_NumberOfDoors);
            stringBuilderResult.Append(Vehicle.sr_ToStirngEndOfPairDelimiter);

            return stringBuilderResult.ToString();
        }
    }
}
